package com.logistics.system.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.logistics.common.core.constant.Constants;
import com.logistics.system.domain.Contracts;
import com.logistics.system.domain.TransportPlans;
import com.logistics.system.service.IPayTradeService;
import com.logistics.system.service.ITransportPlansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.logistics.common.log.annotation.Log;
import com.logistics.common.log.enums.BusinessType;
import com.logistics.common.security.annotation.RequiresPermissions;
import com.logistics.system.domain.Waybills;
import com.logistics.system.service.IWaybillsService;
import com.logistics.common.core.web.controller.BaseController;
import com.logistics.common.core.web.domain.AjaxResult;
import com.logistics.common.core.utils.poi.ExcelUtil;
import com.logistics.common.core.web.page.TableDataInfo;

/**
 * 运单Controller
 * 
 * @author logistics
 * @date 2024-06-23
 */
@RestController
@RequestMapping("/waybills")
public class WaybillsController extends BaseController
{
    @Autowired
    private IWaybillsService waybillsService;
    @Autowired
    private ITransportPlansService transportPlansService;
    /**
     * 查询运单列表
     */
    @RequiresPermissions("system:waybills:list")
    @GetMapping("/list")
    public TableDataInfo list(Waybills waybills)
    {
        startPage();
        List<Waybills> list = waybillsService.selectWaybillsList(waybills);
        return getDataTable(list);
    }

    /**
     * 导出运单列表
     */
    @RequiresPermissions("system:waybills:export")
    @Log(title = "运单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Waybills waybills)
    {
        List<Waybills> list = waybillsService.selectWaybillsList(waybills);
        ExcelUtil<Waybills> util = new ExcelUtil<Waybills>(Waybills.class);
        util.exportExcel(response, list, "运单数据");
    }

    /**
     * 获取运单详细信息
     */
    @RequiresPermissions("system:waybills:query")
    @GetMapping(value = "/{waybillId}")
    public AjaxResult getInfo(@PathVariable("waybillId") Long waybillId)
    {
        return success(waybillsService.selectWaybillsByWaybillId(waybillId));
    }

    /**
     * 新增运单
     */
    @RequiresPermissions("system:waybills:add")
    @Log(title = "运单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Waybills waybills)
    {
        TransportPlans transportPlans = transportPlansService.selectTransportPlansByPlanId((waybills.getPlanId()));
        if(transportPlans.getPlanStatus().equals(Constants.PLAN_STATUS_1)){
            return AjaxResult.error("已生成运单，无法创建运单");
        }
        transportPlans.setPlanStatus(Constants.PLAN_STATUS_1);
        transportPlansService.updateTransportPlans(transportPlans);
        return toAjax(waybillsService.insertWaybills(waybills));
    }

    /**
     * 修改运单
     */
    @RequiresPermissions("system:waybills:edit")
    @Log(title = "运单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Waybills waybills)
    {
        return toAjax(waybillsService.updateWaybills(waybills));
    }

    /**
     * 删除运单
     */
    @RequiresPermissions("system:waybills:remove")
    @Log(title = "运单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{waybillIds}")
    public AjaxResult remove(@PathVariable Long[] waybillIds)
    {
        return toAjax(waybillsService.deleteWaybillsByWaybillIds(waybillIds));
    }

    /**
     * 司机接单
     */
    @RequiresPermissions("system:waybills:takeOrder")
    @Log(title = "运单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/takeOrder/{waybillId}")
    public AjaxResult takeOrder(@PathVariable Long waybillId) {
        Waybills waybills = waybillsService.selectWaybillsByWaybillId(waybillId);
        if(!waybills.getWaybillStatus().equals(Constants.WAYBILL_STATUS_0)){
            return AjaxResult.error("该运单不是未运输状态，禁止司机接单");
        }
        waybills.setWaybillStatus(Constants.CONTRACTS_STATUS_1);
        return toAjax(waybillsService.updateWaybills(waybills));
    }
    /**
     * 运单完成
     */
    @RequiresPermissions("system:waybills:transportComplete")
    @Log(title = "运单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/transportComplete/{waybillId}")
    public AjaxResult transportComplete(@PathVariable Long waybillId) {
        Waybills waybills = waybillsService.selectWaybillsByWaybillId(waybillId);
        if(!waybills.getWaybillStatus().equals(Constants.WAYBILL_STATUS_1)){
            return AjaxResult.error("该运单不是运输中状态，禁止运单完成");
        }
        waybills.setWaybillStatus(Constants.CONTRACTS_STATUS_2);
        return toAjax(waybillsService.updateWaybills(waybills));
    }


}
